@extends('admin.common.base-master')

@section('html-head')
    {{--<link href="{{url('/vendor/tyupload/control/css/zyUpload.css')}}" type="text/css" rel="stylesheet" />--}}
    <link href="{{url('css/jquery.form.css')}}" type="text/css" rel="stylesheet" />
    {{--<script src="{{url('/vendor/My97DatePicker/WdatePicker.js')}}"></script>--}}
    <script src="{{url('/js/lib/jquery.form.js')}}"></script>

    <link rel="stylesheet" href="{{url('css/admin/jquery-ui.css')}}">
    <script type="text/javascript" src="{{url('css/admin/jquery-ui.min.js')}}"></script>
    <link href="{{url('js/lib/jquery-ui-timepicker-addon.css')}}" type="text/css" />
    <script src="{{url('js/lib/jquery-ui-timepicker-addon.js')}}" type="text/javascript"></script>
    <script src="{{url('js/lib/jquery.ui.datepicker-zh-CN.js')}}" type="text/javascript"></script>
    <script src="{{url('js/lib/jquery-ui-timepicker-zh-CN.js')}}" type="text/javascript"></script>

    <!-- 配置文件 -->
    <script type="text/javascript" src="{{url('/ueditor/ueditor.config.js')}}"></script>
    <!-- 编辑器源码文件 -->
    <script type="text/javascript" src="{{url('/ueditor/ueditor.all.js')}}"></script>
    <style>
        .btn_add_classify{
            background: #31bcd3;margin-left:10px;height: 5px;width: 50px;color: white;text-align: center;padding:5px;border-radius:7px;box-shadow: 3px 2px 9px #888888;
        }
    </style>
@endsection

@section('html-body')
    <!-- content start -->
    <div id="content">
        <!-- cur-position start -->
        <div id="cur-position">
            <span class="tit">当前位置：</span>
            <a href="{{route('Admin.Home.couponsList')}}">规则管理</a>
            <em>></em>
            <a href="#">促销设置</a>
        </div>
        <!-- cur-position end -->
        @include('admin.common.discount-header')

        <div class="gg-step">
            <form  id="advform">
                <input type="hidden" name="id" id="id" value="@if($thisRule){{$thisRule->id}}@endif"/>
                <div class="gg-list">
                    <label class="tit">促销名称</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入名称" name="name" id="name" value="@if($thisRule){{$thisRule->name}}@endif" /><cite id="title_re" class="required">*</cite>
                        <p class="text">输入促销名称</p>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">促销类型</label>
                    <div class="cont">
                        <select name="type" class="gg-ipt" id="type" onChange="javascript:trans(this.value)">
                            <option value="0">点击选择促销类型</option>
                            <option value="buy_get_free" @if($thisRule && $thisRule->type == 'buy_get_free')selected @endif>满数量-赠送</option>
                            <option value="reduce" @if($thisRule && $thisRule->type == 'reduce')selected @endif>满数量-订单减金额</option>
                            <option value="discount" @if($thisRule && $thisRule->type == 'discount')selected @endif>满数量-订单总价打折</option>
                        </select>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">适用范围</label>
                    <div class="cont">
                        <select name="range" class="gg-ipt" id="range">
                            <option value="vip" @if($thisRule && $thisRule->range == 'vip')selected @endif>会员用户</option>
                            <option value="all" @if($thisRule && $thisRule->range == 'all')selected @endif>所有用户</option>
                        </select>
                    </div>
                </div>

{{--                <div class="gg-list">--}}
{{--                    <label class="tit">订单满金额</label>--}}
{{--                    <div class="cont">--}}
{{--                        <input class="gg-ipt" type="text" placeholder="请输入满金额" name="satisfy_amount" id="satisfy_amount" value="@if($thisRule){{$thisRule->satisfy_amount}}@endif" /><cite id="title_re" class="required">*</cite>--}}
{{--                        <p class="text">单笔订单满足该金额后，才可享受促销活动，对金额无限制可以不填，或填0</p>--}}
{{--                    </div>--}}
{{--                </div>--}}

                <div class="gg-list" >
                    <label class="tit">商品满数量</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入满数量" name="satisfy_number" id="satisfy_number" value="@if($thisRule){{$thisRule->satisfy_number}}@endif" /><cite id="title_re" class="required">*</cite>
                        <p class="text">单个商品（同一商品的不同款式、尺码、颜色也算一个商品）满足该数量后，才可享受促销活动</p>
                    </div>
                </div>
                <hr>

                <div class="gg-list" id="1" @if($thisRule && $thisRule->type != 'buy_get_free') style="display: none" @endif>
                    <label class="tit">赠送数量</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入赠送数量" name="free_get_number" id="free_get_number" value="@if($thisRule){{$thisRule->free_get_number}}@endif" /><cite id="title_re" class="required">*</cite>
                        <p class="text">满足上述条件后，赠送的商品数量</p>
                    </div>
                </div>

                <div class="gg-list" id="2" @if($thisRule && $thisRule->type != 'reduce') style="display: none" @endif>
                    <label class="tit">减金额</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入订单优惠的金额" name="reduce_amount" id="reduce_amount" value="@if($thisRule){{$thisRule->reduce_amount}}@endif" /><cite id="title_re" class="required">*</cite>
                        <p class="text">满足上述条件后，订单减掉的金额</p>
                    </div>
                </div>

                <div class="gg-list" id="3" @if($thisRule && $thisRule->type != 'discount') style="display: none" @endif>
                    <label class="tit">总价打折</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入订单的折扣" name="reduce_rate" id="reduce_rate" value="@if($thisRule){{$thisRule->reduce_rate}}@endif" /><cite id="title_re" class="required">*</cite>
                        <p class="text">满足上述条件后，订单的折扣，7折输入0.7，85折输入0.85，系统保留2位小数</p>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">开始时间</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请选择发放开始时间" autocomplete="off" name="start_time" id="start_time" value="@if($thisRule){{$thisRule->start_time}}@endif" /><cite id="title_re" class="required">*</cite>
                        <p class="text">选择合适的时间</p>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">结束时间</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请选择发放结束时间" autocomplete="off" name="end_time" id="end_time" value="@if($thisRule){{$thisRule->end_time}}@endif" /><cite id="title_re" class="required">*</cite>
                        <p class="text">选择合适的时间</p>
                    </div>
                </div>
                


                
                <div class="gg-list">
                    <label class="tit">状态</label>
                    <div class="cont">
                        <select name="status" id="status">
                            <option value="1" @if($thisRule && $thisRule->status == 1)selected @endif>启用</option>
                            <option value="0" @if($thisRule && $thisRule->status == 0)selected @endif>不启用</option>
                        </select>
                    </div>
                </div>

            </form>
            <div class="gg-list" >
                <label class="tit">&nbsp;</label>
                <div class="cont" >
                    <a href="javascript:;" class="next-step" id="btn-set">提交</a>
                </div>
            </div>
        </div>
    </div>

    <!-- content end -->
    <script>
        function trans(selected) {
            if (selected == "buy_get_free") {
                $('#1').show();
                $('#2').hide();
                $('#3').hide();
            }
            if (selected == "reduce") {
                $('#1').hide();
                $('#2').show();
                $('#3').hide();
            }
            if (selected == "discount"){
                $('#1').hide();
                $('#2').hide();
                $('#3').show();
            }
        }
        $(function() {
            jQuery('#start_time').datetimepicker({
                timeFormat: "HH:mm:ss",
                dateFormat: "yy-mm-dd",
                beforeShow: function () {
                    setTimeout(
                            function () {
                                $('#ui-datepicker-div').css("z-index", 1000);
                            }, 100
                    );
                }
            });

            jQuery('#end_time').datetimepicker({
                timeFormat: "HH:mm:ss",
                dateFormat: "yy-mm-dd",
                beforeShow: function () {
                    setTimeout(
                            function () {
                                $('#ui-datepicker-div').css("z-index", 1000);
                            }, 100
                    );
                }
            });


            $(document).on('click', '#btn-set', function () {
                var name             = $('#name').val();
                var type             = $('#type option:selected').val();
                var satisfy_number   = $('#satisfy_number').val();
                var range            = $('#range option:selected').val();
                // var satisfy_money        = $('#satisfy_money').val();

                var free_get_number = $('#free_get_number').val();
                var reduce_amount   = $('#reduce_amount').val();
                var reduce_rate     = $('#reduce_rate').val();
                var startTime       = $('#start_time').val();
                var endTime         = $('#end_time').val();
                var status          = $('#status option:selected').val();
                var id              = $('#id').val();

                $.ajax({
                    url: '{{route("Api.Admin.sys.setDiscountSalesRule")}}',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        name: name,
                        type:type,
                        satisfy_number:satisfy_number,
                        range:range,
                        // satisfy_money:satisfyMoney,
                        free_get_number:free_get_number,
                        reduce_amount:reduce_amount,
                        reduce_rate:reduce_rate,
                        start_time: startTime,
                        end_time:endTime,
                        status:status,
                        id:id
                    },
                    success: function (obj) {
                        if (obj.level == "success") {
                            noty({
                                text: obj.message, type: obj.level, timeout: 600,
                                callback: {
                                    afterClose:function(){
                                        window.location.href = "{{route('Admin.Home.discountSalesList')}}";
                                    }
                                }
                            });
                        } else if (obj.level == "error") {
                            noty({text: obj.message, type: obj.level, timeout: 600});
                        }

                    },
                    error: function (obj) {
                        console.log(obj);
                    }
                });
            });

        });
    </script>
@endsection